//评论
$('.yej-content-comment .reply').click(function(){
    var $comment = $(this).parents('.yej-content-comment');
    var pid = $comment.attr('data-pid');
    var author = $comment.attr('data-author');

    var $reply_box = $comment.find('.yej-reply-wrap');
    if($reply_box.length==0){
        $reply_box = $(
            '<div class="yej-reply-wrap">' +
            '<textarea name="reply-to" placeholder="回复' +
            author +
            '"></textarea> ' +
            '<a class="btn btn-submit">确定</a> ' +
            '<a class="btn btn-cancel">取消</a> ' +
            '</div>');
        $reply_box.attr('data-time',new Date().getTime());

        $comment.append($reply_box);
        $reply_box.slideDown('fast');
    }
    var time = $reply_box.attr('data-time');
    $('.yej-reply-wrap').each(function(){
        if($(this).attr('data-time')!= time)$(this).remove();
    });
    $reply_box.find('textarea').eq(0).focus();

    $reply_box.find('.btn-submit').click(function(){
        //评论
        var $comment = $(this).parents('.yej-content-comment');
        var pid = $comment.attr('data-pid');
        var to_uid = $comment.attr('data-authorid');

        var _textarea = $reply_box.find('textarea').eq(0);
        var _comment = _textarea.val();
        if($.trim(_comment)==''){
            antd.message.error('评论不能为空！');
            _textarea.focus();
            return false;
        }

        var url = getBaseUrl() + '/mengwu/mengwuApi/yuerjing_post_replyPost/type/'+yej_post_type;
        var data = {
            pid: pid,
            tid: yej_post_tid,
            to_uid: to_uid,
            comment: _comment
        };
        _post(url,jQuery.param(data),{data_back: function(data){
            if(data.status){
                antd.message.success('评论成功！');
                setTimeout(function(){
                    window.location.reload();
                },1000);
            }else{
                if(typeof data.info.redirect !="undefined"){
                    antd.message.error(data.info.info);
                    setTimeout(function(){
                        window.location.href = data.info.redirect;
                    },1000);
                }else{
                    antd.message.error('评论失败！');
                }
            }
        }});
    });
    $reply_box.find('.btn-cancel').click(function(){
        $reply_box.slideUp('fast',function(){
            $reply_box.remove();
        });

    });

});

//回复
$('.yej-reply .btn-pinglun').click(function(){
    var _textarea = $('.yej-reply').find('textarea').eq(0);
    var _comment = _textarea.val();
    if($.trim(_comment)==''){
        antd.message.error('回复不能为空！');
        _textarea.focus();
        return false;
    }
    var url = getBaseUrl() + '/mengwu/mengwuApi/yuerjing_post_reply/type/'+yej_post_type;
    var data = {
        tid: yej_post_tid,
        message: _comment
    };
    _post(url,jQuery.param(data),{data_back: function(data){
        if(data.status){
            antd.message.success('回复成功！');
            setTimeout(function(){
                window.location.reload();
            },1000);
        }else{
            if(typeof data.info.redirect !="undefined"){
                antd.message.error(data.info.info);
                setTimeout(function(){
                    window.location.href = data.info.redirect;
                },1000);
            }else{
                antd.message.error('回复失败！');
            }
        }
    }});
});

//收藏
$('.ifav').click(function(){
    var $ifav = $(this).find('span').eq(0);
    var ifav_text = $ifav.text();
    var url = getBaseUrl() + '/mengwu/mengwuApi/post_favorites';
    var favorites = false;
    var $ele = $(this);

    var data = {
        tid: yej_post_tid,
        favorite_value : 1
    };
    if(ifav_text == '已收藏'){
        data.favorite_value = 0;
    }else{
        favorites = true;
    }

    $ele.addClass('disabled').attr('disabled','disabled');

    _post(url,jQuery.param(data),{data_back: function(data){
        if(data.status){
            if(favorites){
                $('.ifav').find('span').text('已收藏');
                antd.message.success('收藏成功！');
            }else{
                $('.ifav').find('span').text('收藏');
                antd.message.success('取消成功！');
            }
        }else{
            if(typeof data.info.redirect !="undefined"){
                antd.message.error(data.info.info);
                setTimeout(function(){
                    window.location.href = data.info.redirect;
                },1000);
            }else{
                antd.message.error('收藏失败！');
            }
        }
        $ele.removeClass('disabled').removeAttr('disabled');
    }});
});

//关注
$('.btn-focus').click(function(){
    var $focus = $(this);
    var focus_text = $focus.text();
    var url = getBaseUrl() + '/mengwu/mengwuApi/user_focus';
    var focus = false;

    var data = {
        focus_uid : yej_post_authorid,
        value : 1
    };
    if(focus_text == '已关注'){
        data.value = 0;
    }else{
        focus = true;
    }

    $focus.addClass('disabled').attr('disabled','disabled');

    _post(url,jQuery.param(data),{data_back: function(data){
        if(data.status){
            if(focus){
                $('.btn-focus').text('已关注');
                antd.message.success('关注成功！');
            }else{
                $('.btn-focus').text('取消关注');
                antd.message.success('取消成功！');
            }
        }else{
            if(typeof data.info.redirect !="undefined"){
                antd.message.error(data.info.info);
                setTimeout(function(){
                    window.location.href = data.info.redirect;
                },1000);
            }else{
                antd.message.error('关注失败！');
            }
        }
        $focus.removeClass('disabled').removeAttr('disabled');
    }});

});